﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliQuartz
 * 唯一标识：9e3bde55-7544-49cf-a9dd-cbaa39e65e23
 * 文件名：WcsTask
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/6/21 10:57:42
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
//using KingerRobot.MexicoSailunMDC.Dao;
using System.Diagnostics;

namespace KingerRobot.MexicoSailunQuartz;

public class SyncMes
{
    private readonly static object _Lock = new object();


    //定时同步产品信息
    public void SyncProduct()
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始同步胎胚信息" });
        try
        {
            var mesService = K8.RSF.Client.Create<IMesService>();
            var prodService = K8.RSF.Client.Create<IProductService>();
            var taskService = K8.RSF.Client.Create<ITaskService>();
            var bcdInfos = mesService.GetMesNoSyncProduct();
            lock (_Lock)
            {
                foreach (var bcdInfo in bcdInfos)
                {
                    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"MESPROD开始添加：{bcdInfo.BcdNo}" });
                    var result = prodService.GetMesNoSyncProduct(bcdInfo);
                    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"MESPROD添加完成：{bcdInfo.BcdNo}" });
                    if (result == 1)
                    {
                        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始修改RCVFLAG：{bcdInfo.BcdNo}" });
                        mesService.UpdateBcdInfoRcvFlag(bcdInfo.BcdNo);
                        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"修改RCVFLAG完成：{bcdInfo.BcdNo}" });
                    }
                }
            }
        
        
            lock (_Lock)
            {
                var list = mesService.GetRequestMaterialInfos();
                foreach (var item in list)
                {
                    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"胎高开始添加：{item.TBM_SPEC}-{item.TCM_HEIGHT}" });
                    taskService.UpdateHeight(item.TBM_SPEC, item.TCM_HEIGHT);
                    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"胎高开始添加完成：{item.TBM_SPEC}-{item.TCM_HEIGHT}" });
                }
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"同步胎胚信息异常-{ex.ToString()}" });
        }
    }
    //定时同步产品质量
    public void SyncProductLock()
    {
        K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始同步胎胚状态" });
        try
        {
            var mesService = K8.RSF.Client.Create<IMesService>();
            var prodService = K8.RSF.Client.Create<IProductService>();
            var bcdInfos = mesService.GetMesNoSyncHoldReq();
            if (bcdInfos == null || bcdInfos.Count() == 0)
            {
                return ;
            }
            foreach (var bcdInfo in bcdInfos)
            {
                var result = prodService.GetMesNoSyncHoldReq(bcdInfo);
                if (result > 0)
                {
                    mesService.UpdateHoldReqRcvFlag(bcdInfo.BcdNo);
                }
            }
        }
        catch (Exception ex)
        {
            K8.LSF.Client.Log(new K8.LSF.Log { Data = $"同步胎胚状态异常-{ex.ToString()}" });
        }

    }


    public void AddInEndByBcdNos()
    {
        //K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始推送入库" });
        //try
        //{
        //    var mesService = K8.RSF.Client.Create<IMesService>();
        //    var bcdNo = "245144144247";
        //    var locCode = "123";
        //    mesService.AddInEndByBcdNos(bcdNo, locCode);
        //}
        //catch (Exception ex)
        //{
        //    K8.LSF.Client.Log(new K8.LSF.Log { Data = $"开始推送入库异常-{ex.ToString()}" });
        //}
    }

    public void FlushCuringStatus()
    {
        
        var service = K8.RSF.Client.Create<IEmsPickService>();
        
        service.FlushCuringScadaStatus();
    }
}
